package com.chang.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: changzhenxi
 * @date: 2025年03月05日 14:39
 * @Version: 1.0
 * @description: 容器中的binding，queue，Exchange使用@Bean后都会自动创建
 */
@Configuration
public class MyMQConfig {



    // 死信队列
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "order-event-exchange");  // 死信队列中消息过期之后发送至指定交换机
        args.put("x-dead-letter-routing-key", "order.release");  // 死信消息发送到交换机中指定路由key
        args.put("x-message-ttl", 120000); // 队列中消息过期时间  2分钟
        return new Queue("order.delay.queue", true, false, false, args);
    }

    // 普通队列,用于接收 死信队列中延迟消息的重分配
    @Bean
    public Queue orderReleaseQueue() {
        return new Queue("order.release.queue", true, false, false);
    }

    @Bean
    public Exchange orderExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateBinding() {
        return new Binding("order.delay.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.create", null);
    }

    @Bean
    public Binding orderReleaseBinding() {
        return new Binding("order.release.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release", null);
    }

    // 订单释放直接和库存释放进行绑定
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding("stock.release.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.release.other.#", null);
    }

    // 秒杀队列
    @Bean
    public Queue orderSeckillQueue() {
        return new Queue("order.seckill.queue", true, false, false);
    }

    // 秒杀队列绑定关系
    @Bean
    public Binding orderSeckillQueueBinding() {
        return new Binding("order.seckill.queue", Binding.DestinationType.QUEUE, "order-event-exchange", "order.seckill.order", null);
    }

}
